package com.wangshouyu.mp.offiaccount.message;

import com.wangshouyu.mp.base.request.HttpMethod;
import com.wangshouyu.mp.base.request.MpClient;
import com.wangshouyu.mp.base.request.RequestBody;
import com.wangshouyu.mp.base.response.BaseResponse;
import com.wangshouyu.mp.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.MessageFormat;

import static com.wangshouyu.mp.base.constant.UrlConstant.CUSTOM_SEND_URL;
import static com.wangshouyu.mp.base.constant.UrlConstant.TYPING_URL;

/**
 * @author 王守钰
 * @date 2020年12月07日 14:11
 * @description 客服消息处理
 */
public class KfMessageHandler {

    private KfMessageHandler() {
    }

    private static final Logger log = LoggerFactory.getLogger(KfMessageHandler.class);

    /**
     * 发送消息
     * @param client 客户端
     * @param message 消息
     * @param accessToken 凭证
     * @return 响应结果
     */
    public static BaseResponse send(MpClient client, BaseKfMessage message, String accessToken){
        String url = MessageFormat.format(CUSTOM_SEND_URL, accessToken);
        return client.request(HttpMethod.POST,
                url,
                RequestBody.create(GsonUtils.toJson(message)),
                BaseResponse.class);
    }

    /**
     * 命令消息
     * @param client 客户端
     * @param message 命令消息
     * @param accessToken 凭证
     * @return 结果
     */
    public static BaseResponse command(MpClient client, KfCommandMessage message, String accessToken){
        String url = MessageFormat.format(TYPING_URL, accessToken);
        return client.request(HttpMethod.POST,
                url,
                RequestBody.create(GsonUtils.toJson(message)),
                BaseResponse.class);
    }
}
